package com.sr.util;

import org.joda.time.DateTime;
import org.joda.time.DateTimeConstants;
import org.joda.time.Days;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.util.Date;

/**
 * @author xiaochi
 * @date 2022/4/27 16:52
 * @desc JodaTimeUtil
 */
public class JodaTimeUtil {

    private static final String PATTERN_DATE_TIME = "yyyy-MM-dd HH:mm:ss";
    private static final String PATTERN_DATE = "yyyy-MM-dd";
    private static final String PATTERN_TIME = "HH:mm:ss";

    /**
     * Date 转 字符串
     * @param date
     * @return
     */
    public static String date2Str(final Date date){
        if (date == null){
            return null;
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(PATTERN_DATE_TIME);
    }

    /**
     * Date 转 字符串
     * @param date
     * @return
     */
    public static String date2Str(final Date date,final String formatPattern){
        if (date == null){
            return null;
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(formatPattern);
    }

    /**
     * 字符串 转 Date
     * @param date
     * @return
     */
    public static Date str2Date(final String date){
        if (date == null || "".equals(date)){
            return null;
        }
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(PATTERN_DATE_TIME);
        return dateTimeFormatter.parseDateTime(date).toDate();
    }

    /**
     * 判断date日期是否过期(与当前时刻比较)
     * @param date
     * @return
     */
    public static boolean isTimeExpired(final Date date){
        if (date ==  null){
            return false;
        }
        return isBeforeNow(date2Str(date));
    }

    /**
     * 判断date日期是否过期(与当前时刻比较)
     * @param date
     * @return
     */
    public static boolean isTimeExpired(final String date){
        if (date ==  null || "".equals(date)){
            return false;
        }
        return isBeforeNow(date);
    }

    /**
     * 判断date日期是否过期(与当前时刻比较)
     * @param date
     * @return
     */
    public static boolean isBeforeNow(final String date){
        if (date ==  null || "".equals(date)){
            return false;
        }
        return DateTime.parse(date,DateTimeFormat.forPattern(PATTERN_DATE_TIME)).isBeforeNow();
    }

    /**
     * 加天数
     * @param date
     * @param days
     * @return
     */
    public static Date plusDays(final Date date,Integer days){
        if (date ==  null){
            return null;
        }
        days = days == null ? 0 : days;
        return new DateTime(date).plusDays(days).toDate();
    }

    /**
     * 加天数
     * @param date
     * @param days
     * @return
     */
    public static Date plusDays(final String date,Integer days){
        if (date ==  null || "".equals(date)){
            return null;
        }
        days = days == null ? 0 : days;
        return DateTimeFormat.forPattern(PATTERN_DATE_TIME).parseDateTime(date).plusDays(days).toDate();
    }

    /**
     * 减天数
     * @param date
     * @param days
     * @return
     */
    public static Date minusDays(final Date date,Integer days){
        if (date ==  null){
            return null;
        }
        days = days == null ? 0 : days;
        return new DateTime(date).minusDays(days).toDate();
    }

    /**
     * 加分钟
     * @param date
     * @param minutes
     * @return
     */
    public static Date plusMinutes(final Date date,Integer minutes){
        if (date ==  null){
            return null;
        }
        minutes = minutes == null ? 0 : minutes;
        return new DateTime(date).plusMinutes(minutes).toDate();
    }

    /**
     * 减分钟
     * @param date
     * @param minutes
     * @return
     */
    public static Date minusMinutes(final Date date,Integer minutes){
        if (date ==  null){
            return null;
        }
        minutes = minutes == null ? 0 : minutes;
        return new DateTime(date).minusMinutes(minutes).toDate();
    }

    /**
     * 加月份
     * @param date
     * @param months
     * @return
     */
    public static Date plusMonths(final Date date,Integer months){
        if (date ==  null){
            return null;
        }
        months = months == null ? 0 : months;
        return new DateTime(date).plusMonths(months).toDate();
    }

    /**
     * 减月份
     * @param date
     * @param months
     * @return
     */
    public static Date minusMonths(final Date date,Integer months){
        if (date ==  null){
            return null;
        }
        months = months == null ? 0 : months;
        return new DateTime(date).minusMonths(months).toDate();
    }

    /**
     * 判断date是否在开始和结束时间之间
     * @param date
     * @param startDate
     * @param endDate
     * @return
     */
    public static boolean minusMonths(final Date date,final Date startDate,final Date endDate){
        if (null == date || null == startDate || null == endDate) {
            return false;
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.isBefore(endDate.getTime()) && dateTime.isAfter(startDate.getTime());
    }

    /**
     * 计算两个时间相差多少天
     * @param startDate
     * @param endDate
     * @return
     */
    public static int diffDay(final Date startDate,final Date endDate){
        if (null == startDate || null == endDate) {
            return 0;
        }
        int days = Days.daysBetween(new DateTime(startDate), new DateTime(endDate)).getDays();
        return Math.abs(days);
    }

    /**
     * 获取当前系统时间
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String getCurrentDateTime() {
        return new DateTime().toString(PATTERN_DATE_TIME);
    }

    /**
     * 获取当前日期
     * @return
     */
    public static String getCurrentDate() {
        return new DateTime().toString(PATTERN_DATE);
    }

    /**
     * 获取当前是一周星期几
     * @return
     */
    public static String getWeek() {
        DateTime dts = new DateTime();
        String week = null;
        switch (dts.getDayOfWeek()) {
            case DateTimeConstants.SUNDAY:
                week = "星期日";
                break;

            case DateTimeConstants.MONDAY:
                week = "星期一";
                break;

            case DateTimeConstants.TUESDAY:
                week = "星期二";
                break;
            case DateTimeConstants.WEDNESDAY:
                week = "星期三";
                break;
            case DateTimeConstants.THURSDAY:
                week = "星期四";
                break;
            case DateTimeConstants.FRIDAY:
                week = "星期五";
                break;
            case DateTimeConstants.SATURDAY:
                week = "星期六";
            default:
                break;
        }
        return week;
    }

    /**
     * 获取当前是一周星期几
     * @param year
     * @param month
     * @param day
     * @return
     */
    public static String getWeek(final Integer year,final Integer month,final Integer day) {
        LocalDate dts = new LocalDate(year,month,day);
        String week = null;
        switch (dts.getDayOfWeek()) {
            case DateTimeConstants.SUNDAY:
                week = "星期日";
                break;

            case DateTimeConstants.MONDAY:
                week = "星期一";
                break;

            case DateTimeConstants.TUESDAY:
                week = "星期二";
                break;
            case DateTimeConstants.WEDNESDAY:
                week = "星期三";
                break;
            case DateTimeConstants.THURSDAY:
                week = "星期四";
                break;
            case DateTimeConstants.FRIDAY:
                week = "星期五";
                break;
            case DateTimeConstants.SATURDAY:
                week = "星期六";
            default:
                break;
        }
        return week;
    }
}
